package com.yanftch.json2ets.json2ets.parser.typescript

import com.google.gson.JsonArray
import com.google.gson.JsonNull
import com.google.gson.JsonObject
import com.google.gson.JsonPrimitive
import com.jetbrains.rd.generator.nova.util.syspropertyOrInvalid

open class TsPrimitiveConverter : JsonTraver {
    override fun traversePrimitive(asJsonPrimitive: JsonPrimitive, key: String?): String {
        return when {
            asJsonPrimitive.isString -> {
                "string"
            }

            asJsonPrimitive.isNumber -> {
                "number"
            }

            asJsonPrimitive.isBoolean -> {
                "boolean"
            }

            else -> "object"
        }
    }

    override fun traverseNull(jsonNull: JsonNull, key: String?): String {
        return "string" // 如果 JSON的value为null，则将此字段的类型定义为 string.
    }

    fun findJSONObjectWithMostProperties(jsonArray: JsonArray): JsonObject? {
        var maxProperties = 0
        var result: JsonObject? = null

        for (i in 0 until jsonArray.size()) {
            val jsonObject = jsonArray.get(i) as JsonObject
            val propertyCount = jsonObject.size()

            if (propertyCount > maxProperties) {
                maxProperties = propertyCount
                result = jsonObject
            }
        }

        return result
    }

    override fun traverseArray(jsonArray: JsonArray, key: String?): String {
        var ret = ""
        if (jsonArray.size() > 0) {
            // 遍历找到 field 最多的一个 Object，进行解析
//            val jsonItemValue = findJSONObjectWithMostProperties(jsonArray) ?: JsonObject()
            var jsonItemValue = jsonArray.get(0)
            if (jsonItemValue.isJsonObject) {
                jsonItemValue = jsonItemValue as com.google.gson.JsonObject
                jsonArray.forEach {
                    if (it.isJsonObject && !it.isJsonArray) {
                        val tmp = it as com.google.gson.JsonObject
                        for (key in tmp.keySet()) {
                            println("Key: $key")
                            System.out.println("Value: " + tmp.get(key))
                            if (!jsonItemValue.has(key)) {
                                jsonItemValue.add(key, tmp.get(key))
                            }
                        }
                    }
                }
                System.out.println("finalJsonObject的合并大小：" + jsonItemValue.size())
            }

            if (jsonItemValue.isJsonObject && !jsonItemValue.isJsonArray) {
                val objectType = traverseSingleObject(jsonItemValue.asJsonObject, key)
                ret = "$objectType[]"
            } else if (jsonItemValue.isJsonPrimitive) {
                val type = traversePrimitive(jsonItemValue.asJsonPrimitive, null)
                ret = "$type[]"
            }
            return ret
        }
        return "object[]"
    }

    override fun traverseSingleObject(jsonObject: JsonObject, key: String?): String {
        return "object"
    }

    protected fun toCamelcase(vararg names: String): String {
        return names.reduce { acc, string ->
            (uppercaseFirstChar(acc) + uppercaseFirstChar(string)).replace(" ", "")
        }
    }

    private fun uppercaseFirstChar(key: String): String = key[0].toUpperCase() + key.substring(1, key.length)


    val TYPE_OBJECT = "Object"

    protected fun getDefaultValueByType(type: String?): Pair<String, Any> {
        /**
         * string
         * number
         * boolean
         * BBBBData
         * BBBBDataItems[]
         */
        if (type == null) return Pair("obj", TYPE_OBJECT)
        return when (type) {
            "string" -> Pair(type, "\"\"")
            "number" -> Pair(type, 0)
            "boolean" -> Pair(type, false)
             else -> {
                 if (type.contains("[]")) {
                     return Pair("[]", "[]")
                 } else {
                     return Pair("obj", "new $type()")
                 }
             }
        }
    }
}
